//
//  UIView_Ex.swift
//  DawnFoundation
//
//  Created by 崔成成 on 2021/8/14.
//

import UIKit

public extension DawnWrapper where Base: UIView {

    init(frame: CGRect) {
        base = .init(frame: frame)
    }
    
    func addSubviews(sub views: [UIView]) {
        views.forEach({ base.addSubview($0) })
    }
    
    func randomColor() {
        base.backgroundColor = .rgb(r: CGFloat(arc4random()%255),
                                    g: CGFloat(arc4random()%255),
                                    b: CGFloat(arc4random()%255))
    }

    func corner(radius: CGFloat = 5,
                  borderWidth: CGFloat = 0,
                  backgroundColor: UIColor = .white,
                  borderColor: UIColor? = nil) {
        let imageView = UIImageView(image: UIImage.drawRectWithRoundedCorner(radius: radius,
                                                             borderWidth: borderWidth,
                                                             backgroundColor: backgroundColor,
                                                             borderColor: borderColor,
                                                             size: base.bounds.size))
        base.insertSubview(imageView, at: 0)
    }
    
    @discardableResult
    func line(height: CGFloat = 1,
              color: UIColor = .hex(0x999999),
              insets: UIEdgeInsets = .zero) -> UIView {
        if let v = base.viewWithTag(1099) {
            return v
        }
        let la = UIView()
        la.tag = 1099
        la.backgroundColor = color
        la.frame = CGRect(x: insets.left,
                          y: base.bounds.height-height-insets.bottom,
                          width: base.bounds.width-insets.horizontal,
                          height: height)
        base.addSubview(la)
        return la
    }
        
    func dot(num: Int? = nil) {
        let lbl = base.viewWithTag(2099) as? UILabel ?? UILabel()
        lbl.font = .font(12)
        lbl.textAlignment = .center
        lbl.textColor = .white
        lbl.backgroundColor = .red
        if lbl.superview == nil {
            base.addSubview(lbl)
        }
        var width = 6.adapter
        if let num = num, num > 0 {
            width = 10.adapter
            lbl.text = String(num)
        }else {
            lbl.text = nil
        }
        lbl.top(0).right(base.bounds.width).width(width).height(width)
        lbl.layer.cornerRadius = lbl.bounds.width/2
    }
    
    func removeAllSubviews() {
        base.subviews.forEach({ $0.removeFromSuperview() })
    }
    
    func createImg() -> UIImage? {
        let rect = base.frame
        UIGraphicsBeginImageContext(rect.size)
        base.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    var vc: UIViewController? {
        var _next = base.next
        while _next != nil {
            if let _vc = _next as? UIViewController {
                return _vc
            }
            _next = _next?.next
        }
        return nil
    }
}

///动画
extension UIView {
    func rock() {
        layer.removeAllAnimations()
        let _origin = center
        let keyAnimation:CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position")
        //开始时间
        keyAnimation.beginTime = CACurrentMediaTime()
        //持续时间 0.5
        keyAnimation.duration = 0.1
        keyAnimation.values = [
            NSValue(cgPoint: CGPoint(x: _origin.x - 8, y: _origin.y - 5)),
            NSValue(cgPoint: CGPoint(x: _origin.x + 5, y: _origin.y + 3)),
        ]
        //重复次数 3
        keyAnimation.repeatCount = 3
        keyAnimation.autoreverses = true
        //完成后是否移除
        keyAnimation.isRemovedOnCompletion = true
        layer.add(keyAnimation, forKey: "position")
    }
}

extension CALayer {
    func addCorner(_ corner: CGFloat, top: CGFloat = 0, color: UIColor = .white, roundRect: UIRectCorner = .allCorners) {
        let la = CAShapeLayer()
        la.frame = bounds
        let path = UIBezierPath(roundedRect: CGRect(x: 0, y: top, width: bounds.width, height: bounds.height-top), byRoundingCorners: roundRect, cornerRadii: CGSize(width: corner, height: corner))
        path.close()
        la.path = path.cgPath
        mask = la
        backgroundColor = color.cgColor
    }
    
    
    
}
